<?php
/*
*  -------------------------------------------------
*   @file		: .php
*   @link		:CPC  www.chinapet.com
*   @copyright	: 2002-2012 Xingmo Inc
*   @author		: Laipiyang <462166282@qq.com>
*   @blog_url   : http://www.icuir.com/
*   @blog_url   : http://blog.sina.com.cn/xmphp
*   @date		:
*   @update		:
*  -------------------------------------------------
*/
include_once './include/init.inc.php';
$filename=basename(__FILE__);
//checklogin();
$act=isset ( $_GET['act'] ) ? trim ( $_GET['act'] ) : '' ;
if($act=='add')
{
	  $smarty->assign('sort_list',$db->fetch_all("SELECT * FROM `info_q_usual_sort` WHERE `type`='1'"));
	  $smarty->assign('a',array('title'=>'勋章添加页面','act'=>'insert','btn'=>'勋章添加完成'));
	  $smarty->display(ADMIN_TPL.'medal_info.dwt');

}
else if ($act=='insert')
{
	 /*
	 Array
(
    [medal_name] => sdfdfdf
    [sort_id] => 15
    [order] => 45
    [enable] => 1
    [B1] => 勋章添加完成
    [medal_id] =>
    [coupon_licuicui] =>
)
    [photo] => Array
        (
            [name] => 648191eeg99a552cffa74&690.jpg
            [type] => image/pjpeg
            [tmp_name] => C:\Windows\Temp\php4EB2.tmp
            [error] => 0
            [size] => 72103
        )

	 */

	 $medal_name=isset ( $_POST['medal_name'] ) ? trim ( $_POST['medal_name'] ) : '' ;
	 $sort_id=isset ( $_POST['sort_id'] ) ? intval ( $_POST['sort_id'] ) : 0 ;
	 $order=(isset($_POST['order']) && $_POST['order']<>'') ? intval($_POST['order']) : 20;
	$enable=isset ( $_POST['enable'] ) ? trim ( $_POST['enable'] ) :1 ;
	$photo=isset($_FILES['photo']) ? $_FILES['photo'] : array();
	$dateline=time();
    if(strlen($medal_name)<4 || strlen($medal_name)>30)
    {
		alert('勋章名称过长或者不合规...');
		exit;
    }
	if($db->fetch_one("SELECT * FROM `info_q_medal` WHERE `medal_name`='{$medal_name}'"))
	{
		alert('该道具已经存在,请重新输入');
		exit;
	}
	if($sort_id==0)
	{
		alert('勋章分类必选...');
		exit;
	}
	if(!empty($photo['name']))
	{
		if($photo['error']=='0')
		{
			if(is_uploaded_file($photo['tmp_name']))
			 {
				 $file_dir=ROOT.'/upload/medal/';
				 $upload=new UploadFile();
				 $imgs_name=$upload->upload($file_dir,$photo,2000000);  //默认传2M每张图片控制
			 }
		}
	}
	else
	{
		alert('勋章图片必选');
		exit;
	}
	//sql insert
	 $sql="INSERT INTO `info_q_medal` (`medal_name`,`sort_id`,`order`,`enable`,`photo`,`dateline`) VALUES ('{$medal_name}','{$sort_id}','{$order}','{$enable}','{$imgs_name}','{$dateline}') ";
	 if($db->query($sql))
	 {
		alert('勋章添加成功',$filename);
		exit;
	 }
	 else
	 {
		alert('勋章添加失败',$filename);
		exit;
	 }
}
else if ($act=='edit')
{
   $medal_id=isset ( $_GET['medal_id'] ) ? intval ( $_GET['medal_id'] ) : 0 ;
   if($medal_id<1)
   {
	 alert('操作非法');
	 exit;
   }
   $sql="SELECT * FROM `info_q_medal` WHERE `medal_id`='{$medal_id}'";
   $query=$db->fetch_one($sql);
	if(!$query)
	{
		 alert('数据不存在');
		 exit;
	}
	echo '<pre>';
	print_r ($query);
	echo '</pre>';
	$smarty->assign('sort_list',$db->fetch_all("SELECT * FROM `info_q_usual_sort` WHERE `type`='1'"));
	$smarty->assign('r',$query);
	$smarty->assign('a',array('title'=>'勋章修改页面','act'=>'update','btn'=>'勋章修改完成'));
	$smarty->display(ADMIN_TPL.'medal_info.dwt');
}
else if ($act=='update')
{
	 $medal_id=isset ( $_POST['medal_id'] ) ? intval ( $_POST['medal_id'] ) :0;
	 if($medal_id<1)
	 {
		alert('操作非法');
		exit;
	 }
	$medal_licuicui=isset ( $_POST['medal_licuicui'] ) ? trim ( $_POST['medal_licuicui'] ) : '' ;
	$medal_name=isset ( $_POST['medal_name'] ) ? trim ( $_POST['medal_name'] ) : '' ;
	$sort_id=isset ( $_POST['sort_id'] ) ? intval ( $_POST['sort_id'] ) : 0 ;
	$order=(isset($_POST['order']) && $_POST['order']<>'') ? intval($_POST['order']) : 20;
	$enable=isset ( $_POST['enable'] ) ? trim ( $_POST['enable'] ) :1 ;
	$photo=isset($_FILES['photo']) ? $_FILES['photo'] : array();
	$dateline=time();
	if(strlen($medal_name)<4 || strlen($medal_name)>30)
    {
		alert('勋章名称过长或者不合规...');
		exit;
    }
	if($db->fetch_one("SELECT * FROM `info_q_medal` WHERE `medal_name`='{$medal_name}' AND `medal_id`<>'{$medal_id}'"))
	{
		alert('该道具已经存在,请重新输入');
		exit;
	}
	if($sort_id==0)
	{
		alert('勋章分类必选...');
		exit;
	}

    $sql="UPDATE `info_q_medal` SET `medal_name`='{$medal_name}', `sort_id`='{$sort_id}',`order`='{$order}',`enable`='{$enable}',`dateline`='{$dateline}' ";
    if(!empty($photo['name']))
	{
		if($photo['error']=='0')
		{
			if(is_uploaded_file($photo['tmp_name']))
			 {
				 $upload=new UploadFile();   //实例化上传类
				 $file_dir = ROOT.'/upload/medal/';  //设置上传目录
				if($imgs_name=$upload->upload($file_dir,$photo,2000000))
				{
					 $sql.=" ,`photo`='{$imgs_name}' ";
				}
			 }
		}
	}
    $sql.=" WHERE `medal_id`='{$medal_id}' ";
	$res=$db->query($sql);
	if($res)
	{
		alert('勋章更新成功',$filename);
		exit;
	}
	else
	{
		alert('勋章更新失败',$filename);
		exit;
	}

}
else if ($act=='del')
{
    $medal_id=isset ( $_GET['medal_id'] ) ? intval ( $_GET['medal_id'] ) : 0;
	if($medal_id<1)
	{
		alert('操作非法');
		exit;
	}
	$sql="SELECT * FROM `info_q_medal` WHERE `medal_id`='{$medal_id}' ";
	$query=$db->fetch_one($sql);
	if(!$query)
	{
		alert('数据不存在');
		exit;
	}
//	$file_dir=ROOT.'upload/medal/'.substr($query['photo'],0,2);
//	if(is_dir($file_dir))
//	{
//		delDirAndFile($file_dir);
//	}
	$sql="DELETE FROM `info_q_medal` WHERE `medal_id`='{$medal_id}' ";
	if($db->query($sql))
	{
		alert('勋章删除成功',$filename);
		exit;
	}
	else
	{
		alert('勋章删除失败',$filename);
		exit;
	}

}
else if ($act=='ajax')
{
	$medal_id=isset ( $_POST['medal_id'] ) ? intval ( $_POST['medal_id'] ) : 0;
	if($medal_id<1)
	{
		alert('操作非法');
		exit;
	}
	$sql="SELECT * FROM `info_q_medal` WHERE `medal_id`='{$medal_id}'";
	$query=$db->fetch_one($sql);
	if(!$query)
	{
		echo 3;
		exit;
	}
	echo $a=$query['enable'] ? 0 : 1 ;
	$sql="UPDATE `info_q_medal` SET `enable`='{$a}' WHERE `medal_id`='{$medal_id}' ";
	$db->query($sql);
}
else
{

	$smarty->assign('pl',$pl=licuicui_page($db->num_rows($db->query("SELECT * FROM `info_q_medal`")),$filename,9));
	$smarty->assign('medal_list',$db->fetch_all("SELECT m.*,s.`sort_name` FROM `info_q_medal` AS m LEFT JOIN `info_q_usual_sort` AS s ON m.`sort_id`=s.`sort_id`  LIMIT {$pl['start']},{$pl['pagesize']}"));
	$smarty->assign('web',array('title'=>'勋章列表'));
	$smarty->display(ADMIN_TPL.'medal_list.dwt');

}
?>